package org.ddd.book.interfaces.eventsubscribe;

import com.google.common.eventbus.Subscribe;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ddd.book.book.BookService;
import org.ddd.book.book.BookroomService;
import org.ddd.book.book.BookshelfService;
import org.ddd.book.domain.book.event.BookroomRemoveEvent;
import org.ddd.book.domain.book.event.BookshelfRemoveEvent;
import org.ddd.book.domain.book.event.LibraryRemoveEvent;
import org.springframework.stereotype.Component;

/**
 * 图书事件消费者
 */
@Component
@Slf4j
@AllArgsConstructor
public class BookEventSubscribe extends BaseEventBusSubscribe {
    private final BookroomService bookroomService;
    private final BookshelfService bookshelfService;
    private final BookService bookService;

    /**
     * 图书馆移除事件
     *
     * @param event 图书馆移除事件
     */
    @SuppressWarnings("all")
    @Subscribe
    public void onLibraryRemoveEvent(LibraryRemoveEvent event) {
        log.info("receive LibraryRemoveEvent! libraryId = {} , eventType = {} , subEventType = {}", event.getLibraryId(), event.eventType().getName(), event.subEventType().getName());
        // 移除所有图书馆拥有的图书室
        bookroomService.removeLibraryAllBookroom(event.getLibraryId());
    }

    /**
     * 图书室移除事件
     *
     * @param event 图书室移除事件
     */
    @SuppressWarnings("all")
    @Subscribe
    public void onBookroomRemoveEvent(BookroomRemoveEvent event) {
        log.info("receive BookroomRemoveEvent! libraryId = {} , eventType = {} , subEventType = {}", event.getRoomId(), event.eventType().getName(), event.subEventType().getName());
        // 移除所有图书室拥有的书架
        bookshelfService.removeBookroomAllBookshelf(event.getRoomId());
    }

    /**
     * 书架移除事件
     *
     * @param event 书架移除事件
     */
    @SuppressWarnings("all")
    @Subscribe
    public void onBookshelfRemoveEvent(BookshelfRemoveEvent event) {
        log.info("receive BookshelfRemoveEvent! libraryId = {} , eventType = {} , subEventType = {}", event.getBookshelfId(), event.eventType().getName(), event.subEventType().getName());
        // 移除所有书架拥有的图书
        bookService.removeBookshelfAllBook(event.getBookshelfId());
    }

}
